package android.zhcs.myapplication;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.zhcs.myapplication.databinding.ActivityMainActivity2syBinding;
import android.zhcs.myapplication.databinding.ChatboxBinding;
import android.zhcs.myapplication.databinding.ItemBinding;
import android.zhcs.myapplication.ui.dashboard.DashboardFragment;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity2sy extends AppCompatActivity {


    private int now;
    private ArrayList<bean1> list1 = new ArrayList<bean1>();
    public static ArrayList<bean1> list2 = new ArrayList<bean1>();
    public static double zjg;
    private ActivityMainActivity2syBinding binding;
    private ItemBinding binding1;
    private ChatboxBinding inflate;
    private ItemBinding binding2;

    public static void start(Context c) {
        Intent intent = new Intent(c, MainActivity2sy.class);
        c.startActivity(intent);
    }

    private void gbjg() {
        list2 = new ArrayList<>();
        zjg = 0;
        for (bean1 bean1 : list1) {
            if (bean1.num != 0) {
                list2.add(bean1);
                zjg += bean1.jg * bean1.num;
            }
        }
        String format = new DecimalFormat("#.##").format(zjg);
        binding.zjg.setText("￥"+format);

    }


    private void gbjg1() {
        list2 = new ArrayList<>();
        zjg = 0;
        for (bean1 bean1 : list1) {
            if (bean1.num != 0) {
                list2.add(bean1);
                zjg += bean1.jg * bean1.num;
            }
        }
        String format = new DecimalFormat("#.##").format(zjg);
        inflate.zjg.setText("￥"+format);
        binding.zjg.setText("￥"+format);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        binding = ActivityMainActivity2syBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        list1.add(new bean1("鱼香肉丝1",R.mipmap.dbz,1.2));
        list1.add(new bean1("鱼香肉丝2",R.mipmap.dbz,2.2));
        list1.add(new bean1("鱼香肉丝3",R.mipmap.dbz,3.2));
        list1.add(new bean1("鱼香肉丝4",R.mipmap.dbz,4.2));
        list1.add(new bean1("鱼香肉丝5",R.mipmap.dbz,5.2));
        list1.add(new bean1("鱼香肉丝6",R.mipmap.dbz,6.2));
        binding.fh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        binding.gwc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(MainActivity2sy.this);
                inflate = ChatboxBinding.inflate(getLayoutInflater());
                bottomSheetDialog.setContentView(inflate.getRoot());
                inflate.zjg.setText("￥" + zjg);
                inflate.js.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        PreferenceManager.getDefaultSharedPreferences(MainActivity2sy.this).edit().putString("data",new Gson().toJson(list2)).apply();
                        PreferenceManager.getDefaultSharedPreferences(MainActivity2sy.this).edit().putString("zjg",zjg+"").apply();
                        bottomSheetDialog.cancel();
                        MainActivity2zf.start(MainActivity2sy.this);
                    }
                });
                inflate.mrec.seta(1, list2.size(), R.layout.item, new Cal() {
                    @Override
                    public void bix(View itemView, int position) {
                        bean1 b = list2.get(position);
                        ItemBinding binding1 = ItemBinding.bind(itemView);
                        binding1.name.setText(b.name);
                        binding1.num.setText(b.num+"");
                        binding1.jg.setText("￥" + b.jg);
                        binding1.img.setImageResource(b.img);
                        binding1.jia.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                list1.get(position).num++;
                                binding1.num.setText(b.num + "");
                                gbjg1();
                            }
                        });
                        binding1.jian.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if (b.num == 0) {
                                    return;
                                }
                                list1.get(position).num--;
                                binding1.num.setText(b.num + "");
                                gbjg1();
                            }
                        });
                    }
                });
                bottomSheetDialog.show();
            }
        });
        binding.vp.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @NotNull
            @Override
            public Fragment createFragment(int position) {
                return new DashboardFragment(position);
            }

            @Override
            public int getItemCount() {
                return 2;
            }
        });
        binding.zjg.setText("￥"+zjg);
        binding.vp.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                now = position;
                super.onPageSelected(position);
            }
        });
        Timer timer = new Timer();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        binding.vp.setCurrentItem(now++);
                        if (now == 2) now = 0;

                    }
                });
            }
        };
//        timer.schedule(timerTask, 0, 1000);
        binding.mrec.seta(1, list1.size(), R.layout.item, new Cal() {
            @Override
            public void bix(View itemView, int position) {
                bean1 b = list1.get(position);
                ItemBinding binding2 = ItemBinding.bind(itemView);
                binding2.name.setText(b.name);
                binding2.num.setText(b.num+"");
                binding2.jg.setText("￥" + b.jg);
                binding2.img.setImageResource(b.img);
                binding2.jia.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        b.num++;
                        binding2.num.setText(b.num + "");
                        Log.e("zp", binding2.num.getText().toString()+"");
                        gbjg();
                    }
                });
                binding2.jian.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (b.num == 0) {
                            return;
                        }
                        list1.get(position).num--;
                        binding2.num.setText(b.num + "");
                        gbjg();
                    }
                });
            }
        });
    }
}